const multer = require("multer");

const upload = multer({
    storage: multer.diskStorage({
        destination(req, file, cb) {
                    cb(null, './upload')
        },
        filename:function(req, file, cb) {
            //保存的文件名
            cb(null, file.originalname);
        }
    }),
    // 限制上传大小和同时上传的数量
    // limits：{
    //     fileSize
    // }

    //依据mime文件类型过滤
    fileFilter: function(req, file, cb) {
        const allowArr = ["image/jpeg", "image/gif", "image/png", "image/jpg"];
        if(allowArr.indexOf(file.mimetype) > -1) {
            cb(null, true)
        }else {
            cb(new Error("文件类型不正确"))
        }
        
    }
    
})

module.exports = upload

